package com.mfs.common.enums;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.function.Function;
import java.util.zip.DeflaterInputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 压缩枚举类，含压缩、解压方法
 *
 * @author mfs
 * @date 2023/7/23 15:06
 **/
public enum  CompressEnum {
    /*COMPRESS(1, "compress", bytes -> {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream();
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out);) {
            deflaterOutputStream.write(bytes);
            return out.toByteArray();
        } catch (IOException e) {
        }
        return null;
    }, bytes -> {
        return null;
    }),*/
    DEFLATE(2, "deflate", bytes -> {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream(128);) {
            try (DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out);) {
                deflaterOutputStream.write(bytes);
            } catch (IOException e) {
            }
            return out.toByteArray();
        } catch (IOException e) {
        }
        return null;
    }, bytes -> {
        try (ByteArrayInputStream in = new ByteArrayInputStream(bytes);) {
            try (DeflaterInputStream deflaterInputStream = new DeflaterInputStream(in);) {
                byte[] res = new byte[0];
                byte[] bytes1 = new byte[bytes.length * 10];
                int n = 0;
                while ((n = deflaterInputStream.read(bytes1)) > 0) {
                    byte[] newRes = new byte[res.length + n];
                    System.arraycopy(res, 0, newRes, 0, res.length);
                    System.arraycopy(bytes1, 0, newRes, res.length, n);
                    res = newRes;
                }
                return res;
            } catch (IOException e) {
            }
        } catch (IOException e) {
        }
        return null;
    }),
    GZIP(3, "gzip", bytes -> {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream(128);) {
            try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(out);) {
                gzipOutputStream.write(bytes);
            } catch (IOException e) {
            }
            return out.toByteArray();
        } catch (IOException e) {
        }
        return null;
    }, bytes -> {
        try (ByteArrayInputStream in = new ByteArrayInputStream(bytes);) {
            try (GZIPInputStream gzipInputStream = new GZIPInputStream(in);) {
                byte[] res = new byte[0];
                byte[] bytes1 = new byte[bytes.length * 10];
                int n = 0;
                while ((n = gzipInputStream.read(bytes1)) > 0) {
                    byte[] newRes = new byte[res.length + n];
                    System.arraycopy(res, 0, newRes, 0, res.length);
                    System.arraycopy(bytes1, 0, newRes, res.length, n);
                    res = newRes;
                }
                return res;
            } catch (IOException e) {
            }
        } catch (IOException e) {
        }
        return null;
    }),

    /**
     * 不支持的解、压缩方式，直接返回
     * */
    DEFAULT(4, "default", bytes -> bytes, bytes -> bytes);


    private Integer type;
    private String name;
    private Function<byte[], byte[]> compressor;
    private Function<byte[], byte[]> decompressor;
    CompressEnum(Integer type, String name, Function<byte[], byte[]> compressor, Function<byte[], byte[]> decompressor) {
        this.type = type;
        this.name = name;
        this.compressor = compressor;
        this.decompressor = decompressor;
    }

    public Integer getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    public Function<byte[], byte[]> getCompressor() {
        return compressor;
    }

    public Function<byte[], byte[]> getDecompressor() {
        return decompressor;
    }
}
